package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

public class PickFindWorksPopWindow extends BasePopWindow {
    public static final String PACKERS_TYPE = "packers_type";
    private LinearLayout rootLayout;
    private MaxRecyclerView recyclerView;
    private MaxRecyclerView childView;
    private TextView okView;
    private TextView reSetView;
    private FilterResultClick filterResultClick;
    private PickingResultClick pickingResultClick;
    private PickAdapter pickAdapter;
    private PickAdapter pickAdapterCity;
    private PickAdapter pickAdapterType;
    private PickAdapter pickAdapterStatus;
    private PickAdapter pickAdapterPackers;
    private String type;
    //地区
    private List<String> citysNo = new ArrayList<>();
    //规模
    private List<String> scaleNo = new ArrayList<>();
    //类型
    private List<String> typeNo = new ArrayList<>();
    //状态
    private List<String> statusNo = new ArrayList<>();
    //工程类别
    private List<String> packersNo = new ArrayList<>();

    private TextView tvScaleTitle;
    private TextView tvTypeTitle;
    private TextView tvStatusTitle;
    private TextView tvPackersTitle;
    private MaxRecyclerView typeList;
    private MaxRecyclerView statusList;
    private MaxRecyclerView packersList;

    public interface FilterResultClick {
        void result(List<String> citysNo, List<String> scaleNo);

        void dimiss();
    }

    public interface PickingResultClick {
        void result(List<String> citysNo, List<String> typeNo, List<String> scaleNo, List<String> statusNo);

        void dimiss();
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }


    public PickFindWorksPopWindow(Context context, String type) {
        super(context, DIR_RIGHT);
        this.type = type;
        /*if ("CLASS".equals(type)) {
            tvScaleTitle.setVisibility(View.GONE);
        } else {
            tvScaleTitle.setVisibility(View.VISIBLE);
        }*/

        if ("PICKING".equals(type)) {
            tvTypeTitle.setVisibility(View.VISIBLE);
            tvStatusTitle.setVisibility(View.VISIBLE);
        }

        if (PACKERS_TYPE.equals(type)) {
            tvScaleTitle.setVisibility(View.GONE);
            typeList.setVisibility(View.GONE);
            childView.setVisibility(View.GONE);
            statusList.setVisibility(View.GONE);
            tvPackersTitle.setVisibility(View.VISIBLE);
            packersList.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected int popLayout() {
        return R.layout.company_list_popwindow;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        reSetView = holder.getPop().findViewById(R.id.reset_view);

        recyclerView = holder.getPop().findViewById(R.id.filter_list);
        childView = holder.getPop().findViewById(R.id.child_list);
        typeList = holder.getPop().findViewById(R.id.type_list);
        statusList = holder.getPop().findViewById(R.id.status_list);
        packersList = holder.getPop().findViewById(R.id.packers_list);

        tvScaleTitle = holder.getPop().findViewById(R.id.tv_scale_title);
        tvTypeTitle = holder.getPop().findViewById(R.id.tv_pick_type);
        tvStatusTitle = holder.getPop().findViewById(R.id.tv_pick_status);
        tvPackersTitle = holder.getPop().findViewById(R.id.tv_pick_packers);

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if ("PICKING".equals(type)) {
                    pickingResultClick.result(citysNo, typeNo, scaleNo, statusNo);
                } else if (PACKERS_TYPE.equals(type)) {
                    filterResultClick.result(citysNo, packersNo);
                } else {
                    filterResultClick.result(citysNo, scaleNo);
                }
            }
        });

        reSetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                citysNo.clear();
                scaleNo.clear();
                typeNo.clear();
                statusNo.clear();
                packersNo.clear();

                pickAdapter.resetCheckDatas();
                pickAdapterCity.resetCheckDatas();
                pickAdapterType.resetCheckDatas();
                pickAdapterStatus.resetCheckDatas();
                pickAdapterPackers.resetCheckDatas();

                pickAdapter.setDefItem(-1);
                pickAdapterCity.setDefItem(-1);
                pickAdapterType.setDefItem(-1);
                pickAdapterStatus.setDefItem(-1);
                pickAdapterPackers.setDefItem(-1);
            }
        });

        pickAdapterCity = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        //pickAdapterCity.setMultiSelect(true);
        pickAdapterCity.setItemSelect(true);
        pickAdapterCity.setDefItem(-1);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(pickAdapterCity);

        pickAdapterCity.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                citysNo.clear();
                citysNo.add(pickSelectEntity.getNo());

            }
        });
        /*pickAdapterCity.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> pickSelectEntities, int position) {
                citysNo.clear();
                for (PickSelectEntity pickSelectEntity : pickSelectEntities) {
                    citysNo.add(pickSelectEntity.getNo());
                }
            }
        });*/

        pickAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        //pickAdapter.setMultiSelect(true);
        pickAdapter.setItemSelect(true);
        pickAdapter.setDefItem(-1);
        childView.setLayoutManager(new GridLayoutManager(mContext, 3));
        childView.setAdapter(pickAdapter);

        pickAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                scaleNo.clear();
                scaleNo.add(pickSelectEntity.getNo());
            }
        });

        /*pickAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> pickSelectEntities, int position) {
                scaleNo.clear();
                for (PickSelectEntity pickSelectEntity : pickSelectEntities) {
                    scaleNo.add(pickSelectEntity.getNo());
                }
            }
        });*/

        pickAdapterType = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        //pickAdapterType.setMultiSelect(true);
        pickAdapterType.setItemSelect(true);
        pickAdapterType.setDefItem(-1);
        typeList.setLayoutManager(new GridLayoutManager(mContext, 3));
        typeList.setAdapter(pickAdapterType);

        pickAdapterType.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                typeNo.clear();
                typeNo.add(pickSelectEntity.getNo());
            }
        });

        /*pickAdapterType.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> pickSelectEntities, int position) {
                scaleNo.clear();
                for (PickSelectEntity pickSelectEntity : pickSelectEntities) {
                    typeNo.add(pickSelectEntity.getNo());
                }
            }
        });*/

        pickAdapterStatus = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        //pickAdapterStatus.setMultiSelect(true);
        pickAdapterStatus.setItemSelect(true);
        pickAdapterStatus.setDefItem(-1);
        statusList.setLayoutManager(new GridLayoutManager(mContext, 3));
        statusList.setAdapter(pickAdapterStatus);

        pickAdapterStatus.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                statusNo.clear();
                statusNo.add(pickSelectEntity.getNo());
            }
        });

        /*pickAdapterStatus.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> pickSelectEntities, int position) {
                scaleNo.clear();
                for (PickSelectEntity pickSelectEntity : pickSelectEntities) {
                    statusNo.add(pickSelectEntity.getNo());
                }
            }
        });*/

        pickAdapterPackers = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        pickAdapterPackers.setItemSelect(true);
        pickAdapterPackers.setDefItem(-1);
        packersList.setLayoutManager(new GridLayoutManager(mContext, 3));
        packersList.setAdapter(pickAdapterPackers);

        pickAdapterPackers.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                packersNo.clear();
                packersNo.add(pickSelectEntity.getNo());
            }
        });
    }

    public void setAreaData(List<PickSelectEntity> datas) {
        pickAdapterCity.setDatas(datas);
    }

    public void setScaleData(List<PickSelectEntity> datas) {
        pickAdapter.setDatas(datas);
    }

    public void setTypeData(List<PickSelectEntity> datas) {
        pickAdapterType.setDatas(datas);
    }

    public void setStatusData(List<PickSelectEntity> datas) {
        pickAdapterStatus.setDatas(datas);
    }

    public void setPackersData(List<PickSelectEntity> datas) {
        pickAdapterPackers.setDatas(datas);
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (filterResultClick != null) {
            filterResultClick.dimiss();
        }
    }
}
